<?php
namespace app\api\controller;

class Permission extends Api
{
    protected $token = '';
    public function _initialize()
    {
        parent::_initialize();

        $token = $_SERVER['HTTP_X_TOKEN'];
        
        //验证token
        if( ! $token )
        {
            $result = array(
                'code' => 50008,
                'data' => '登录凭证已失效，请从新登陆',
            );
            $this->res($result);
        }

        //10分钟内不活动就默认为退出
        $act_time = time() - 180000000;
        $map['token'] = $token;
        // $map['cate'] = $this->appid;
        $map['last_act_time'] = ['>',$act_time];
        
        if( !$this->user = $user = db('Admin')->where($map)->find() )
        {
            $result = array(
                'code' => 50008,
                'data' => '登录凭证已失效，请从新登陆',
            );
            $this->res($result);
        }
        else
        {

            $user['last_act_time'] = time();
            db('Admin')->update($user);
        }

        # 获取当前用户第一个站点、到处都要用
        $this->myAllAreaIds = [];
        $map = [];
        $map['admin_id'] = $this->user['id'];
        $area = db('AdminArea')->where($map)->select(); 
        $allIds = [];
        foreach ($area as $v) {
            $allIds[] = $v['area_id'];
        }
        $this->myAllAreaIds = $allIds;
    }

    

}
